#ifndef RESOURCESMANAGER_H
#define RESOURCESMANAGER_H

#include "d3dUtil.h"
#include "StringUtil.h"
#include "Logger.h"

#include "Camera.h"
#include "SceneNode.h"

#include "VertexFormat.h"
#include "Mesh.h"
#include "Material.h"
#include "MaterialInfo.h"
#include "Bone.h"
#include "Face.h"
#include "MaterialSet.h"
#include "VertexWeight.h"

//Assimp stuff
#include <assimp.hpp>      // C++ importer interface
#include <aiScene.h>       // Output data structure
#include <aiPostProcess.h> // Post processing flags


class ResourcesManager
{
public:
	~ResourcesManager(void);

	//initialize the resource manager ,must be called first
	static void Init(ID3D10Device* device);

	//return the singleton of resource manager
	static ResourcesManager* Instance();

	//get the texture by name,if failed return NULL
	ID3D10ShaderResourceView*	GetTexture(const std::string& texName);

	//get mesh by mesh id
	Mesh*		GetMeshByID(int id);

	//load the mode ,if success return the mode's root else return null
	SceneNode*	LoadModelFile(const std::string& name);

	//input the material info,return the pointer to the material,return null if the material not exist
	Material* GetMaterialByInfo(const MaterialInfo* info);

	void BuildMeshBuffer();

	//clear all the resource
	void Clear();

private:
	void LogSceneInfo(const aiScene* pScene);


	ID3D10ShaderResourceView* CreateTexture(const std::string& texName);

	//singleton stuff
	void SetDevice(ID3D10Device* device);
	ResourcesManager(void);
	ResourcesManager(const ResourcesManager&);
	ResourcesManager& operator=(const ResourcesManager&);

private:
	MaterialSet*		ReadMaterialSetFromScene(const aiScene* scene);
	
	void LoadNodeWithMeshes(aiNode* node,SceneNode* sceneNode,SceneNode* parent=NULL);

	//make mesh from aiMesh
	Mesh* MakeMesh(const aiMesh* mesh,int MaterialSetID ,int ID);

	//ai* to mesh stuff
	inline D3DXCOLOR	Convert_aiColor_To_D3DXCOLOR(const aiColor3D& color);	
	inline D3DXVECTOR3	Convert_aiVector3D_To_D3DXVECTOR3(const aiVector3D& vec);
	inline D3DXVECTOR3	Convert_aiVector3D_To_D3DXVECTOR3(const aiVector3D* vec);
	inline Face			Convert_aiFace_To_Face(const aiFace& face);
	inline void			Convert_aiMatrix_To_D3DXMATRIX(const aiMatrix4x4& aiMat,D3DXMATRIX& mat);

private:
	ID3D10Device*										m_pDevice;
	int													m_iMeshIDBase;
	std::map<std::string,ID3D10ShaderResourceView*>		m_TextureMap;
	std::vector<MaterialSet*>							m_lstMaterialSet;
	std::vector<Mesh*>									m_lstMesh;
};



#endif

